package com.xiguanlezz.cn.common;

import com.xiguanlezz.cn.auth.AuthOperation;
import com.xiguanlezz.cn.auth.AuthOperationResult;
import com.xiguanlezz.cn.keepalive.KeepaliveOperation;
import com.xiguanlezz.cn.keepalive.KeepaliveOperationResult;
import com.xiguanlezz.cn.order.OrderOperation;
import com.xiguanlezz.cn.order.OrderOperationResult;
import lombok.Getter;

/**
 * @Author：习惯了种种
 * @CreateDate：2024/10/16 22:44
 * @Description：操作类型枚举类
 */
@Getter
public enum OperationType {
    AUTH(1, AuthOperation.class, AuthOperationResult.class),
    KEEP_ALIVE(2, KeepaliveOperation.class, KeepaliveOperationResult.class),
    ORDER(3, OrderOperation.class, OrderOperationResult.class);


    private int opType;
    private Class<? extends Operation> operationClazz;
    private Class<? extends OperationResult> operationResultClazz;

    OperationType(int opType, Class<? extends Operation> operationClazz, Class<? extends OperationResult> operationResultClazz) {
        this.opType = opType;
        this.operationClazz = operationClazz;
        this.operationResultClazz = operationResultClazz;
    }

    public static OperationType fromOpType(int opType) {
        OperationType[] values = values();
        for (OperationType type : values) {
            if (type.getOpType() == opType) {
                return type;
            }
        }
        return null;
    }

    public static OperationType fromOperation(Operation operation) {
        OperationType[] values = values();
        for (OperationType type : values) {
            if (type.getOperationClazz().isInstance(operation)) {
                return type;
            }
        }
        return null;
    }
}
